import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/users/Users.vue'
// import Rights from '../components/power/Rights.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path:'/login', component:Login},
    { path:'/', redirect:'/login'},
    { path:'/home', 
      redirect:'/welcome',
      component:Home,
      children:[      
        { path:'/welcome',component:Welcome}, 
        { path:'/users',component:Users}
      ]}    
  ]
})

router.beforeEach((to, from , next) => {
  const tokenStr = window.sessionStorage.getItem('token')
  if(to.path === '/login'){
    next()
  } else if(!tokenStr){
    next('/login')
  } else {
    next()
  }
})

export default router